package com.csxy.ljr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.ljr.entity.EduChapter;
import com.csxy.ljr.entity.EduVideo;
import com.csxy.ljr.entity.vo.ChapterVo;
import com.csxy.ljr.entity.vo.VideoVo;
import com.csxy.ljr.mapper.EduChapterMapper;
import com.csxy.ljr.service.EduChapterService;
import com.csxy.ljr.service.EduVideoService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        ArrayList<ChapterVo> finalChapterVos = new ArrayList<>();
//        先根据课程id查询所有章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(queryWrapper);
//        在根据课程id查询所有小节
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(queryWrapper1);
//        拼接章节和小节chapterList[{},{}]
        for (EduChapter chapter : chapterList){
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalChapterVos.add(chapterVo);

            List<VideoVo> finalVideoList = new ArrayList<>();
            for (EduVideo video : videoList){
                if (chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    finalVideoList.add(videoVo);
                }
                chapterVo.setChildren(finalVideoList);
            }
        }
        return finalChapterVos;
    }
}
